
package com.mall.common.directive;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.mall.b2c.hessian.CacheService;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

@Repository
public class PicDirectiveModel implements TemplateDirectiveModel {
	@Resource private CacheService cacheService;
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) 
		throws TemplateException, IOException {
		String pic = DirectiveUtils.getString("pic", params);
		String postfix = DirectiveUtils.getString("postfix", params);
		
		if(StringUtils.isBlank(pic)) {
			pic = cacheService.getSystemConfig().getDefaultImg();
		}
		
		/*if(StringUtils.isNotBlank(postfix)){
			String pattern = "(.*)([\\.])(.*)";
			String thumbPath = "$1_" + postfix + "$2$3";

			Pattern p = Pattern.compile(pattern, 2 | Pattern.DOTALL);
			Matcher m = p.matcher(pic);
			if (m.find()) {
				pic = m.replaceAll(thumbPath);
			}
		}*/
		env.getOut().write(pic);
	}
}